﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using IBll.Sys;
using Model.Database;
using Model.Sys;
using Model.Sys.Constant;
using Model.Sys.Enum;
using SearchModel;

namespace Bll
{
    public partial class DataDictionaryHeadBll
    {
        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="total"></param>
        /// <param name="smDataDictionaryHead"></param>
        /// <returns></returns>
        public List<DataDictionaryHead> GetListBySearchPage(int pageIndex, int pageSize, out int total, SMDataDictionaryHead smDataDictionaryHead)
        {
            IQueryable<DataDictionaryHead> dataDictionaryHeadIq = CurrentDal.GetIQueryable();

            dataDictionaryHeadIq = BaseSearch(dataDictionaryHeadIq, smDataDictionaryHead);
            total = dataDictionaryHeadIq.Count();
            dataDictionaryHeadIq = dataDictionaryHeadIq.OrderBy(a => a.Sort).ThenByDescending(a=>a.AddTime);
            dataDictionaryHeadIq = dataDictionaryHeadIq.Skip((pageIndex - 1) * pageSize).Take(pageSize);//分页前必须排序，不然EF报错

            return dataDictionaryHeadIq.ToList();
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="idArr"></param>
        public void ListDelte(string[] idArr)
        {
            CurrentDal.Delete(a => idArr.Contains(a.DataDictionaryHeadId.ToString()));
            CurrentDalSession.DataDictionaryItemDal.Delete(a=>idArr.Contains(a.DataDictionaryHeadId.ToString()));//级联删除
            CurrentDalSession.SaveChanges();
        }

        /// <summary>
        /// 批量禁用
        /// </summary>
        /// <param name="idArr"></param>
        public void ListForbidden(string[] idArr)
        {
            string sql = "update DataDictionaryHead set Disable=1 where DataDictionaryHeadId in (@idStr)";
            string idStr = null;
            StringBuilder idStrSB = new StringBuilder("");
            foreach (var id in idArr)
            {
                idStrSB.Append("'" + id + "'");
                idStrSB.Append(",");
            }
            idStr = idStrSB.ToString();
            idStr = StringHelper.DelLastChar(idStr, ",");
            sql = sql.Replace("@idStr", idStr);
            CurrentDalSession.BaseDalExtend.ExecuteSql(sql);
        }

        /// <summary>
        /// 批量解禁
        /// </summary>
        /// <param name="idArr"></param>
        public void ListNoForbidden(string[] idArr)
        {
            string sql = "update DataDictionaryHead set Disable=0 where DataDictionaryHeadId in (@idStr)";
            string idStr = null;
            StringBuilder idStrSB = new StringBuilder("");
            foreach (var id in idArr)
            {
                idStrSB.Append("'" + id + "'");
                idStrSB.Append(",");
            }
            idStr = idStrSB.ToString();
            idStr = StringHelper.DelLastChar(idStr, ",");
            sql = sql.Replace("@idStr", idStr);
            CurrentDalSession.BaseDalExtend.ExecuteSql(sql);
        }

        /// <summary>
        /// 获取字典项（从缓存读取）
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public List<DataDictionaryItem> GetItemList(DataDictionaryHeadEnum head)
        {
            List<DataDictionaryItem> dataDictionaryItemList=new List<DataDictionaryItem>();

            ICacheBll cacheBll= IocHelper.AutofacResolve<ICacheBll>();
            Cache<List<DataDictionaryHead>> cache= cacheBll.GetCache<List<DataDictionaryHead>>(Constant.CacheKey.DataDictionaryCacheKey);
            List < DataDictionaryHead > dataDictionaryHeadList =(List<DataDictionaryHead>) cache.Value;
            DataDictionaryHead dataDictionaryHead= dataDictionaryHeadList.FirstOrDefault(a => a.DataDictionaryHeadId == (int)head);
            if (dataDictionaryHead != null)
            {
                dataDictionaryItemList = dataDictionaryHead.DataDictionaryItems.ToList();
            }

            return dataDictionaryItemList;
        }

        /// <summary>
        /// 获取数据字典（字典头集合（包含字典项））
        /// </summary>
        /// <returns></returns>
        public List<DataDictionaryHead> GetDataDictionary()
        {
            List<DataDictionaryHead> dataDictionaryHeadList = CurrentDal.GetList(a=>a.Disable!=true);
            foreach (var dataDictionaryHead in dataDictionaryHeadList)
            {
                dataDictionaryHead.DataDictionaryItems = CurrentDalSession.DataDictionaryItemDal.GetList(a=>a.Disable!=true && a.DataDictionaryHeadId==dataDictionaryHead.DataDictionaryHeadId );
            }

            return dataDictionaryHeadList;;
        }

        /// <summary>
        /// 重置数据字典（重置缓存）
        /// </summary>
        public void ResetDataDictionary()
        {
            List<DataDictionaryHead> dataDictionaryHeadList= GetDataDictionary();

            ICacheBll cacheBll = IocHelper.AutofacResolve<ICacheBll>();
            cacheBll.Update(Constant.CacheKey.DataDictionaryCacheKey, dataDictionaryHeadList);
        }


        #region 私有方法
        /// <summary>
        /// 基础查询
        /// </summary>
        /// <param name="dataDictionaryHeadIq"></param>
        /// <param name="smDataDictionaryHead"></param>
        /// <returns></returns>
        private IQueryable<DataDictionaryHead> BaseSearch(IQueryable<DataDictionaryHead> dataDictionaryHeadIq, SMDataDictionaryHead smDataDictionaryHead)
        {
            if (!string.IsNullOrEmpty(smDataDictionaryHead.Name))
            {
                dataDictionaryHeadIq = dataDictionaryHeadIq.Where(a => a.Name.Contains(smDataDictionaryHead.Name));
            }

            return dataDictionaryHeadIq;
        }
        #endregion
    }
}
